﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using cs499.Database;

namespace cs499.TimeSlots
{
    public partial class ListTimeSlots : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            LinkButton timeslotsMenuButton = (LinkButton)Master.FindControl("LoginView").FindControl("TimeSlotsMenuButton");
            timeslotsMenuButton.CssClass = "selected";
        }

        protected void exportTimeSlotButton_Click(object sender, EventArgs e)
        {
            System.Text.StringBuilder exportFile = new System.Text.StringBuilder();

            //add header info
            exportFile.AppendLine("#FIELD INFO");
            exportFile.AppendLine("#Start Time: time that the course in this time slot would start in 24hr format (eg 19:05 for 7:05pm class)");
            exportFile.AppendLine("#Length: length of the course in this time slot in minutes  (eg 80)");
            exportFile.AppendLine("#Days: days of the week the course in this time slot would be offered (eg MW)");
            exportFile.AppendLine("#      M = Monday");
            exportFile.AppendLine("#      T = Tuesday");
            exportFile.AppendLine("#      W = Wednesday");
            exportFile.AppendLine("#      R = Thursday");
            exportFile.AppendLine("#      F = Friday");
            exportFile.AppendLine("#      S = Saturday");
            exportFile.AppendLine("#      U = Sunday");
            exportFile.AppendLine("");

            //add example lines
            exportFile.AppendLine("#EXAMPLES");
            exportFile.AppendLine("#19:00,80,MW");
            exportFile.AppendLine("#15:55,80,TR");
            exportFile.AppendLine("#08:00,50,MTWF");
            exportFile.AppendLine("");
            exportFile.AppendLine("");

            //add field name line
            exportFile.AppendLine("#Start Time,Length,Days");

            Model1Container database = new Model1Container();
            foreach (TimeSlot timeSlot in database.Set<TimeSlot>())
            {
                exportFile.AppendLine(timeSlot.StartTime.ToString() + "," +
                                      timeSlot.Length.ToString() + "," +
                                      TimeSlot.DaystoString(timeSlot.Days));
            }

            Response.ContentType = @"application/x-msdownload";
            Response.AppendHeader("content-disposition", "attachment; filename=TimeSlots.csv");

            Response.Write(exportFile.ToString());
            Response.Flush();
            Response.End();
        }
    }
}